package io.xxx;

/**
 * 无重复字符的最长子串
 * <p>
 * 给定一个字符串 s ，请你找出其中不含有重复字符的 最长子串 的长度。
 */
public class LongestSubstringWithoutRepeatingCharacters {

    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abcabcbb"));
        System.out.println(lengthOfLongestSubstring("bbbbb"));
        System.out.println(lengthOfLongestSubstring("pwwkew"));
        System.out.println(lengthOfLongestSubstring(""));
    }

    public static int lengthOfLongestSubstring(String s) {
        if (s.equals("")) {
            return 0;
        }

        char prev = s.charAt(0);
        int n = 0;

        for (int i = 1; i < s.length(); ) {
            if (s.charAt(i) == prev) {
                prev = s.charAt(++i);
                n = 0;
            } else {
                n++;
                if (n == s.length())
                    break;
            }
        }
        return n;
    }
}
